
package model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;

public class Terme {
   // private int lastNodeId;
    private String word;
    private StringBuilder positions; 
    private HashSet<Noeud> nodes;
    private ArrayList<TermeOccur> occurs;


/*conflit
 * private double compteurSectionsDiff;
    private HashMap<Integer,Integer> compteursSections;
    private ArrayList<Noeud> parents;

    private ArrayList<Double> listTf;

    private HashMap<Integer, ArrayList<TermeOccur>> occurs;
*/
   

    public Terme(String name) {
        this.word = name;
        //this.lastNodeId = 0;
        this.positions = new StringBuilder();
        this.nodes = new HashSet<Noeud>();
        this.occurs = new ArrayList<TermeOccur>();
/*=======
        this.compteurSectionsDiff = 0;
        this.compteursSections = new HashMap<Integer,Integer>();
        this.parents = new ArrayList<Noeud>();
        this.listTf = new ArrayList<Double>();

        this.occurs = new HashMap<Integer, ArrayList<TermeOccur>>();
>>>>>>> .r15*/

    }
    
    public void addOccurence(int nodeId, int pos, Noeud currentNode) {
        // On incrémente le compteur d'occurence dans des sections différentes
        // si le nouveau nodeId est différent du dernier
        // prérequis : le parsage est séquentiel !
        
        this.nodes.add(currentNode);
        this.occurs.add(new TermeOccur(currentNode, this, pos));
        
        /*if (nodeId>lastNodeId) {
            this.nodes.add(currentNode);
            compteurSectionsDiff++;
            lastNodeId = nodeId;
            compteurSectionsDiff++;
            compteursSections.put(nodeId,1);
            occurs.put(nodeId,new ArrayList<TermeOccur>());

        } else {

            compteursSections.put(nodeId, compteursSections.get(nodeId)+1);
        }
        this.occurs.get(nodeId).add(new TermeOccur(this.word,nodeId, pos));*/
        //this.positions.append(nodeId).append('#').append(pos).append(';');
        // old : this.carac.append(doc).append('/').append(xpath).append('#').append(pos).append(':');
    }

    public String getValue() {
        //return "('"+this.word+"','"+this.carac.toString()+"')";
        /*for (Noeud node : nodes){
            this.calculsPoids(node.getLongueur());
        }*/
        this.positions.append("('").append(this.word).append("','");
        for (TermeOccur occur : this.occurs) {
            this.positions.append(occur.getValue());
            this.positions.append(';');
        }
        this.positions.setLength(this.positions.length()-1); //on enlève le ; final
        this.positions.append("')");
        return this.positions.toString();
    }
    
    public String getWord(){
        return this.word;
    }
    /*public int getNbOccParSection(int nodeId){
        return this.compteursSections.get(nodeId);
    }
    public double getCompteurSections(){
        return this.compteurSectionsDiff;
    }*/

    /*private void calculsPoids(int nbTermesDansLaSection) {
        
        for (ArrayList<TermeOccur> list : this.occurs.values()) {
            for (TermeOccur occ : list) {
                occ.tfCalcul(this.compteursSections.get(occ.getNodeId()), nbTermesDansLaSection);
            }
        }
   
        
    }*/
    
    /*public void addTfdsList(double tf){
        this.listTf.add(tf);
    }
    
    public ArrayList<Double> getListTf(){
        return this.listTf;
    }*/
    
    
    /*********************************************
     **   TF = Nb terme i ds section            **
     *              /                           **
     *        Nb terme ds section                   **
     ********************************************/
    /*public double calculFreqTerme(int nodeId, int termeTotalDsSection){
        double val = 0;
        //System.out.println("val nodeId => "+nodeId+" nbTSect => "+getNbOccParSection(nodeId));
        val = (double)getNbOccParSection(nodeId)/ (double)termeTotalDsSection;
        return val;
    }*/
    
   /* public void addNoeudtoList(Noeud n){
        this.listNoeud.add(n);
    }
    
    public ArrayList<Noeud> getListNoeuds(){
        return this.listNoeud;
    }
    
    public void addDocumenttoList(Document doc){
        this.listDoc.add(doc);
    }
    
    public ArrayList<Document> getListDocs(){
        return this.listDoc;
    }
    */
    /*********************************************
     **     IDF = Nb section ds Collection      **
     *                  /                       **
     *      Nb de section contenant le terme i  **
     ********************************************/
    public double calculIDF(int nbSectionTot, int nbSectionAvTi){
        double val = 0;
        val = Math.log((double)nbSectionTot/(double)nbSectionAvTi);
        return val;
    }

    public int getNbSectionsParentes() {
        return this.nodes.size();
    }
}
